package org.example.blogserver.blog.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.blogpojo.Dto.ArticleDto;
import org.example.blogpojo.blog.domain.Article;
import org.example.blogpojo.blog.query.ArticleQuery;
import org.example.blogpojo.blog.vo.GdVo;
import org.example.blogserver.blog.mapper.ArticleMapper;
import org.example.blogserver.blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public List<Article> getAllArticle() {
        return articleMapper.getAllArticle();
    }

    @Override
    public Article getArticleById(Long id) {
        return articleMapper.getArticleById(id);
    }

    @Override
    public PageInfo<Article> queryPage(ArticleQuery articleQuery) {
        System.out.println(articleQuery.toString());
        PageHelper.startPage(articleQuery.getPageNum(), articleQuery.getPageSize());
        List<Article> articles = articleMapper.queryPage(articleQuery);
        return new PageInfo<Article>(articles);
    }

    @Override
    public void insertArticle(Article article) {
        articleMapper.insertArticle(article);
    }

    @Override
    public void updateArticle(Article article) {
        article.setUpdateTime(new Date());
        articleMapper.updateArticle(article);
    }

    @Override
    public void deleteArticle(Long id) {
        articleMapper.deleteArticle(id);
    }

    @Override
    public void deleteArticleByIds(Long[] ids) {
        articleMapper.deleteArticleByIds(ids);
    }

    @Override
    public Article getByFileName(String fileName) {
        return articleMapper.getByFileName(fileName);
    }

    @Override
    public List<GdVo> getGd() {
        return articleMapper.getGd();
    }

    @Override
    public List<Article> getRa(Long id) {
        Article article = articleMapper.getArticleById(id);
        return articleMapper.getRa(article);
    }

    @Override
    public List<ArticleDto> getBlogGdList(String time) {
        List<ArticleDto> blogGdList = articleMapper.getBlogGdList(time);
        Map<String, ArticleDto> map = new HashMap<>();
        for (ArticleDto article : blogGdList) {
            String year = article.getYear();
            if (year != null && !year.equals("null")) {
                if (!map.containsKey(year)) {
                    ArticleDto yearContainer = new ArticleDto(); // 新的容器对象
                    yearContainer.setYear(year); // 只设置年份，不包含具体文章的属性
                    map.put(year, yearContainer);
                }
                // 将当前文章添加到对应年份的容器中
                map.get(year).getArticleDto().add(article);
            }
        }
        List<ArticleDto> list = new ArrayList<>(map.values());
        return list;
    }
}
